<template>
  <div class="file-list">
    <el-table
      :data="files"
      style="width: 100%"
      :row-class-name="getRowClass"
      @row-click="handleRowClick"
      v-loading="loading"
      highlight-current-row
    >
      <!-- 文件名列 -->
      <el-table-column prop="name" label="文件名" min-width="220">
        <template #default="scope">
          <div class="file-name-cell">
            <FileTypeIcon :fileType="scope.row.type" :fileName="scope.row.name" size="small" />
            <el-tooltip 
              :content="scope.row.name" 
              placement="top" 
              :show-after="500"
              :enterable="false"
              effect="dark"
            >
            <span class="file-name">{{ scope.row.name }}</span>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      
      <!-- 类型列 -->
      <el-table-column prop="type" label="类型" width="120">
        <template #default="scope">
          {{ getFileTypeLabel(scope.row.type) }}
        </template>
      </el-table-column>
      
      <!-- 大小列 -->
      <el-table-column prop="size" label="大小" width="120">
        <template #default="scope">
          {{ scope.row.size || '-' }}
        </template>
      </el-table-column>
      
      <!-- 修改时间列 -->
      <el-table-column prop="updateTime" label="修改时间" width="180">
        <template #default="scope">
          {{ formatDate(scope.row.updateTime) }}
        </template>
      </el-table-column>
      
      <!-- 操作列 -->
      <el-table-column label="操作" width="240" fixed="right">
        <template #default="scope">
          <div class="actions-container">
            <!-- 文件信息 (默认显示) -->
            <div class="file-info">
              <span v-if="scope.row.category" class="file-category">{{ scope.row.category }}</span>
              <span v-if="scope.row.description" class="file-description">{{ scope.row.description }}</span>
            </div>
            
            <!-- 操作按钮 (hover时显示) -->
            <div class="file-actions">
              <template v-if="scope.row.type !== 'directory'">
                <!-- 预览按钮 -->
                <el-tooltip content="预览" placement="top" v-if="canPreview(scope.row)">
                  <el-button 
                    type="primary" 
                    size="small" 
                    circle 
                    @click.stop="$emit('preview', scope.row)"
                  >
                    <el-icon><View /></el-icon>
                  </el-button>
                </el-tooltip>
                
                <!-- 在阅读器中打开按钮 -->
                <el-tooltip content="在阅读器中打开" placement="top" v-if="canOpenInReader(scope.row)">
                  <el-button 
                    type="success" 
                    size="small" 
                    circle 
                    @click.stop="$emit('open', scope.row)"
                  >
                    <el-icon><Reading /></el-icon>
                  </el-button>
                </el-tooltip>
                
                <!-- 下载按钮 -->
                <el-tooltip :content="permissionStore.canDownload ? '下载' : permissionStore.downloadDisabledMessage" placement="top">
                  <el-button 
                    type="info" 
                    size="small" 
                    circle 
                    @click.stop="handleDownload(scope.row)"
                    :disabled="!permissionStore.canDownload"
                  >
                    <el-icon><Download /></el-icon>
                  </el-button>
                </el-tooltip>
              </template>
              
              <!-- 详情按钮 -->
              <el-tooltip content="详情" placement="top">
                <el-button 
                  type="warning" 
                  size="small" 
                  circle 
                  @click.stop="$emit('info', scope.row)"
                >
                  <el-icon><InfoFilled /></el-icon>
                </el-button>
              </el-tooltip>
              
              <!-- 删除按钮 -->
              <el-tooltip content="删除" placement="top" v-if="canDelete">
                <el-button 
                  type="danger" 
                  size="small" 
                  circle 
                  @click.stop="$emit('delete', scope.row)"
                >
                  <el-icon><Delete /></el-icon>
                </el-button>
              </el-tooltip>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 空状态 -->
    <el-empty
      v-if="!loading && (!files || files.length === 0)"
      description="没有找到文件"
    />
  </div>
</template>

<script setup>
import { 
  Download, Delete, InfoFilled, View, Reading
} from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { usePermissionStore } from '../../store';
import { computed } from 'vue';
import FileTypeIcon from './FileTypeIcon.vue';
import { getFileTypeLabel } from '../FileExplorer/utils/fileHelpers';

const props = defineProps({
  files: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  },
  canDelete: {
    type: Boolean,
    default: false
  },
  selectedFile: {
    type: Object,
    default: null
  }
});

const emit = defineEmits(['row-click', 'preview', 'download', 'delete', 'info', 'open']);

// 获取权限store
const permissionStore = usePermissionStore();

// 获取行的类名
const getRowClass = (row) => {
  const classes = [];
  
  if (row.row.type === 'directory') {
    classes.push('is-directory');
  }
  
  if (props.selectedFile && props.selectedFile.name === row.row.name && props.selectedFile.type === row.row.type) {
    classes.push('is-selected');
  }
  
  return classes.join(' ');
};

// 处理行点击
const handleRowClick = (row, column, event) => {
  // 阻止事件冒泡
  event.stopPropagation();
  emit('row-click', row);
};

// 判断是否可以预览
const canPreview = (file) => {
  if (file.type === 'directory') return false;
  
  const previewableTypes = ['document', 'markdown', 'image', 'video', 'html', 'presentation', 'spreadsheet'];
  return previewableTypes.includes(file.type) || isPreviewableByExtension(file.name);
};

// 判断是否可以在阅读器中打开
const canOpenInReader = (file) => {
  if (file.type === 'directory') return false;
  
  const openableTypes = ['document', 'markdown', 'html', 'presentation', 'spreadsheet', 'text'];
  return openableTypes.includes(file.type) || isOpenableByExtension(file.name);
};

// 根据扩展名判断是否可预览
const isPreviewableByExtension = (fileName) => {
  const extension = fileName.split('.').pop().toLowerCase();
  const previewableExtensions = [
    'pdf', 'docx', 'doc', 'md', 'markdown', 'jpg', 'jpeg', 'png', 'gif', 
    'mp4', 'webm', 'html', 'htm', 'ppt', 'pptx', 'xls', 'xlsx'
  ];
  return previewableExtensions.includes(extension);
};

// 根据扩展名判断是否可在阅读器中打开
const isOpenableByExtension = (fileName) => {
  const extension = fileName.split('.').pop().toLowerCase();
  const openableExtensions = [
    'pdf', 'docx', 'doc', 'md', 'markdown', 'html', 'htm', 
    'txt', 'json', 'xml', 'ppt', 'pptx', 'xls', 'xlsx'
  ];
  return openableExtensions.includes(extension);
};

// 处理下载按钮点击
const handleDownload = (file) => {
  if (permissionStore.canDownload) {
    emit('download', file);
  } else {
    ElMessage.warning(permissionStore.downloadDisabledMessage);
  }
};

// 格式化日期
const formatDate = (timestamp) => {
  if (!timestamp) return '-';
  
  const date = new Date(timestamp);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
};
</script>

<style scoped>
.file-list {
  width: 100%;
}

.file-name-cell {
  display: flex;
  align-items: center;
  gap: 12px;
}

.file-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}

/* 操作容器 */
.actions-container {
  position: relative;
  height: 32px;
  display: flex;
  align-items: center;
}

/* 文件信息 */
.file-info {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  opacity: 1;
  transition: opacity 0.2s ease;
}

.file-category {
  background-color: #f3f4f6;
  color: #4b5563;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.file-description {
  color: #6b7280;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 文件操作 */
.file-actions {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  display: flex;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.2s ease;
  align-items: center;
}

/* Hover效果 */
.actions-container:hover .file-info {
  opacity: 0;
}

.actions-container:hover .file-actions {
  opacity: 1;
}

/* 目录行样式 */
:deep(.is-directory) {
  background-color: #f8fafc;
}

/* 选中行样式 */
:deep(.is-selected) {
  background-color: #eff6ff !important;
  border-left: 3px solid #3b82f6;
}

/* 表格行hover效果 */
:deep(.el-table__row:hover) {
  background-color: #f0f7ff !important;
}

/* 表头样式 */
:deep(.el-table__header) {
  background-color: #f8fafc;
}

:deep(.el-table__header-row) {
  height: 48px;
}

:deep(.el-table__header-cell) {
  font-weight: 600;
  color: #374151;
}

/* 表格单元格样式 */
:deep(.el-table__cell) {
  padding: 8px 0;
}
</style> 